﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;
using SanQiOther.Models;


public class NavigationHelper
{
    public static string generateNavigation(int roleId)
    {
        RewardDbContext db = new RewardDbContext();

        string navigation = "";

        StringBuilder navbuilder = new StringBuilder();



        var nav = CacheHelper.getCache(AppConfig.CACHE_NAVIGATION + roleId.ToString());
        if (nav == null)
        {

            List<SanQiOther.Models.RolePermissions> moduleid = new List<RolePermissions>();
            moduleid = db.RolePermissions.Where(r => r.roleID == roleId).ToList();
            List<int?> roilidd = new List<int?>();
            roilidd = moduleid.Select(r => r.moduleID).ToList();



            List<SanQiOther.Models.Module> TopModuleId = new List<Module>();
            TopModuleId = db.Module.Where(r => r.parentID == 0 && roilidd.Contains(r.ID) && r.Sq_System == 11).ToList();

            foreach (var roleArea in TopModuleId)
            {
                navbuilder.Append("<li class=\"treeview\">");
                navbuilder.Append("<a href=\"#\"><i class=\"fa " + roleArea.icon + "\"></i> <span>" + roleArea.moduleName + "</span>");
                navbuilder.Append("<span class=\"pull-right-container\">");
                navbuilder.Append("<i class=\"fa fa-angle-left pull-right\"></i>");
                navbuilder.Append("</span>");
                navbuilder.Append("</a>");
                navbuilder.Append("<ul class=\"treeview-menu\">");

                List<SanQiOther.Models.Module> ChildModuleId = new List<Module>();
                ChildModuleId = db.Module.Where(r => r.parentID == roleArea.ID && roilidd.Contains(r.ID)).ToList();

                foreach (var roleController in ChildModuleId)
                {
                    navbuilder.Append("<li><a href=\"" + roleController.url + "\" class=\"mynav\"><i class=\"fa " + roleArea.icon + "\"></i>" + roleController.moduleName + "</a></li>");
                }

                navbuilder.Append("</ul>");
                navbuilder.Append("</li>");
            }

            navigation = navbuilder.ToString();
            CacheHelper.createCache(AppConfig.CACHE_NAVIGATION + roleId.ToString(), navigation);
        }
        else
        {
            navigation = nav.ToString();
        }

        return navigation;
    }

}

